package main

import (
	"fmt"
	"log"
	"net/http"
	_ "project/api"
	"project/pkg/dao/mysql"
	"project/pkg/logger"
	"project/pkg/router"
	"project/pkg/run"
	"project/pkg/setting"
	"project/pkg/snowflake"
	"project/pkg/validate"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// @title project
// @version 1.0
// @description 小程序
// @termsOfService http://swagger.io/terms/

// @contact.name 郝泾钊
//@contact.url
// @contact.email 1141520944@qq.com

// @host 127.0.0.1:8888
// @BasePath /v1
func main() {
	var (
		err error
	)
	if err = setting.Init(); err != nil {
		log.Print("setting error")
		panic(err)
	}
	if err = logger.Init(setting.Conf.LogConfig, setting.Conf.Mode); err != nil {
		log.Fatal(err)
		return
	}
	defer zap.L().Sync()
	//翻译器
	if trans_err := validate.InitTrans("zh"); trans_err != nil {
		zap.L().Error("validate.InitTrans() validator failed;err: %v\n", zap.Error(trans_err))
		return
	}
	//雪花算法
	if snowflake_err := snowflake.Init(setting.Conf.StartTime, setting.Conf.MachineID); snowflake_err != nil {
		zap.L().Error("snowflake.Init() validator failed;err: %v\n", zap.Error(snowflake_err))
		return
	}
	//连接数据库
	if err := mysql.Init(setting.Conf.MySQLConfig); err != nil {
		zap.L().Error("mysql.Init() with fail", zap.Error(err))
		return
	}
	// if err := redis.Init(setting.Conf.RedisConfig); err != nil {
	// 	zap.L().Error("redis.Init() with fail", zap.Error(err))
	// 	return
	// }
	e := gin.Default()
	r := router.Init(e)
	server := &http.Server{Addr: fmt.Sprintf(":%d", setting.Conf.Port), Handler: r}
	run.Run(server)
}
